package com.company;

import com.company.service.Student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class Task5 {
    public static void main(String[] args) {
        //todo 5.  有如下Student 对象，包含三个属性姓名、年龄、分数，班号，
        // 　private String name;
        //    private int age;
        //    private int score;
        //    private String classNum;
        // 其中，classNum 表示学生的班号，例如“class05”。 创建集合并完成下列要求：
        // 1） 计算所有学生的平均年龄
        // 2） 计算各个班级的平均分

        ArrayList list = new ArrayList();
        Student student = new Student();
        student.setName("张三");
        student.setAge(20);
        student.setScore(100);
        student.setClassNum("class01");
        list.add(student);

        Student student2 = new Student();
        student2.setName("李希");
        student2.setAge(39);
        student2.setScore(90);
        student2.setClassNum("class01");
        list.add(student2);

        Student student3 = new Student();
        student3.setName("小七");
        student3.setAge(18);
        student3.setScore(80);
        student3.setClassNum("class02");
        list.add(student3);

        int average = 0;
        HashSet<String> set = new HashSet<String>(list.size());
        HashMap<String,int[]> map =new HashMap();
        for (int i = 0; i < list.size(); i++) {
            Student s = (Student) list.get(i);
            average += s.getAge();
            if (set.add(s.getClassNum())) {
                map.put(s.getClassNum(),new int[]{1,s.getScore()});
            }else{
                int[] i2 = map.get(s.getClassNum());
                i2[0] ++;
                int score = i2[1];
                map.remove(s.getClassNum());
                map.put(s.getClassNum(), new int[]{i2[0],score + s.getScore()});
            }

        }

        System.out.println("所有学生的平均年龄为：" + average/list.size());
        for (Map.Entry<String, int[]> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "平均成绩为:" + entry.getValue()[1] / entry.getValue()[0]);
        }
    }
}
